package faf.web;

import java.rmi.NotBoundException;
import java.rmi.RemoteException;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.validation.BindException;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.SimpleFormController;

import faf.business.PropertiesManager;
import faf.business.QuestionManager;
import faf.business.SheetManager;
import faf.business.SurveyManager;
import faf.entities.Account;
import faf.entities.Properties;
import faf.entities.Question;
import faf.entities.Sheet;
import faf.entities.SheetState;
import faf.entities.Survey;

public class NewQuestionController extends SimpleFormController {

	SurveyManager surveyManager;
	QuestionManager questionManager;
	PropertiesManager propertiesManager;
	SheetManager sheetManager;

	public ModelAndView onSubmit(HttpServletRequest request,
			HttpServletResponse response, Object object, BindException errors)
			throws Exception {

		Account account = (Account) request.getSession().getAttribute("user");
		if (account == null) {
			return new ModelAndView("login");
		}

		Question question = (Question) object;

		Properties properties = new Properties();

		if (request.getParameter("modifier") != null) {
			questionManager.modifyQuestion(question);			
			List<Properties> lt = questionManager.findListProperties(question
					.getId());
			for (Properties current : lt) {
				propertiesManager.deleteProperties(current.getId());
			}

		} else {
			long ida = Long.decode(request.getParameter("surveyFromEdit"));
			Survey survey = surveyManager.get(ida);
			question.setSurvey(survey);
			question.setPosition(1);
			questionManager.createQuestion(question);
		}
		
		long idSurvey = Long.decode(request.getParameter("surveyFromEdit"));
		Survey survey = surveyManager.get(idSurvey);
		List<Sheet> listSheet = sheetManager.list(survey);
		for (Sheet s : listSheet) {
			s.setState(SheetState.REFRESH);
			sheetManager.save(s);
		}

		Enumeration enumeration = request.getParameterNames();
		while (enumeration.hasMoreElements()) {
			String parameterName = (String) enumeration.nextElement();

			char[] chaine = parameterName.toCharArray();
			if (chaine[0] == '_') {
				properties.setValue(request.getParameter(parameterName));
				properties.setQuestion(question);
				propertiesManager.createProperties(properties);
				properties = null;
				properties = new Properties();
			}
		}

		List<Question> LQ = surveyManager.findListQuestion(question.getSurvey()
				.getId());

		List<Sheet> Sheets = sheetManager.numberOfPolled(question.getSurvey());
		int numberOfPolled = Sheets.size();
		List<Account> listAccount = new ArrayList<Account>();
		for (Sheet s : Sheets) {
			listAccount.add(s.getAccount());
		}

		Map<String, Object> surveyModel = new HashMap<String, Object>();
		surveyModel.put("survey", question.getSurvey());
		surveyModel.put("questions", LQ);
		surveyModel.put("size", LQ.size());
		surveyModel.put("listAccount", listAccount);
		surveyModel.put("numberOfPolled", numberOfPolled);

		request.setAttribute("survey", question.getSurvey().getId());

		return new ModelAndView("addquestion", "model", surveyModel);

	}

	public Object formBackingObject(HttpServletRequest request)
			throws ServletException, NotBoundException, RemoteException,
			Exception {

		Question question = new Question();

		if (request.getParameter("modifier") != null) {
			long idm = Long.decode(request.getParameter("modifier"));
			question = questionManager.get(idm);
		}

		return question;

	}

	protected Map<String, Object> referenceData(HttpServletRequest request)
			throws Exception {

		Map<String, Object> propetiesModel = new HashMap<String, Object>();
		if (request.getParameter("modifier") != null) {

			long id = Long.decode(request.getParameter("modifier"));
			List<Properties> LP = questionManager.findListProperties(id);
			propetiesModel.put("size", LP.size());
			propetiesModel.put("properties", LP);
			propetiesModel.put("modification", true);
		}

		return propetiesModel;
	}

	public void setSheetManager(SheetManager sheetManager) {
		this.sheetManager = sheetManager;
	}

	public void setPropertiesManager(PropertiesManager propertiesManager) {
		this.propertiesManager = propertiesManager;
	}

	public void setQuestionManager(QuestionManager questionManager) {
		this.questionManager = questionManager;
	}

	public void setSurveyManager(SurveyManager surveyManager) {
		this.surveyManager = surveyManager;
	}

}
